package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.GwepT;
import cn.e4j.modular.crm.entity.GweqT;
import cn.e4j.modular.crm.service.impl.Gwei022ServiceImpl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 回款配票
 * @author shixc
 * @Date 2018/01/09
 */
@Controller
@RequestMapping("/crm/gwei022")
public class Gwei022Controller extends BaseController {

    @Autowired
    private Gwei022ServiceImpl gwei022Service;

    /**
     * 跳转询价单管理页面
     */
    @RequiresPermissions("crm:gwei022:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwei022.html";
    }

    /**
     * 查询回款配票信息列表
     */
    @RequestMapping("/getGweplist")
    @RequiresPermissions("crm:gwei022:init")
    @ResponseBody
    public ResponseData getGweplist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        /*ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("USER_ID", shiroUser.getID());
        if(shiroUser.getDEVICE_ID().size() > 0){
            inDto.put("has_device_",true);
        }else {
            inDto.put("has_device_",false);
        }*/
        List<Dto> resultDto = this.gwei022Service.getListData(page, inDto);
        for(int i = 0;i<= resultDto.size()-1;i++){
            Dto gwep010 = this.gwei022Service.getGwep010(resultDto.get(i));
            if(gwep010 != null){
                resultDto.get(i).put("GWEP010", gwep010.getString("GWEP010"));
            }else {
                resultDto.get(i).put("GWEP010", "");
            }
            /*Dto azpDto = this.gwpService.getAzpFile(resultDto.get(i).getString("GWEPPLANT"));
            if(azpDto != null) {
                resultDto.get(i).put("GWEPPLANT_NAME", azpDto.getString("AZP02"));
            }else {
                resultDto.get(i).put("GWEPPLANT_NAME", "");
            }*/

        }
        return new PageReData(page, resultDto);
    }

    /**
     * 新增
     */
    @RequestMapping("/addData")
    @RequiresPermissions("crm:gwei022:add")
    @ResponseBody
    public ResponseData addData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwepT> gwepTList = JSONObject.parseArray(jsonObject.getString("list1"), GwepT.class);
        List<GweqT> gweqTList = JSONObject.parseArray(jsonObject.getString("list2"), GweqT.class);
        GwepT gwepT = JSONObject.parseArray(jsonObject.getString("GWEP"),GwepT.class).get(0);
        gwei022Service.addData(gwepTList, gweqTList, gwepT);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/editData")
    @RequiresPermissions("crm:gwei022:edit")
    @ResponseBody
    public ResponseData editData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwepT> gwepTList = JSONObject.parseArray(jsonObject.getString("list1"), GwepT.class);
        List<GweqT> gweqTList = JSONObject.parseArray(jsonObject.getString("list2"), GweqT.class);
        GwepT gwepT = JSONObject.parseArray(jsonObject.getString("GWEP"),GwepT.class).get(0);
        gwei022Service.editData(gwepTList, gweqTList, gwepT);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwei022:del")
    @ResponseBody
    public ResponseData del(GwepT gwepT) {
        gwei022Service.delete(gwepT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwei022:del")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwepT> gwepTList = inDto.getReqList(GwepT.class);
        gwei022Service.delDataBatch(gwepTList);
        return SUCCESS_TIP;
    }

    /**
     * 修改状态
     */
    @RequestMapping("/updateStatus")
    @ResponseBody
    public ResponseData updateStatus(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwepT> gwepTList = JSONObject.parseArray(jsonObject.getString("_xdata"), GwepT.class);
        inDto.put("ACTI",jsonObject.getString("ACTI"));
        inDto.put("ACTI_OLD",jsonObject.getString("ACTI_OLD"));
        gwei022Service.updateStatus(gwepTList,inDto);
        return SUCCESS_TIP;
    }

    /**
     * 获取新增数据
     */
    @RequestMapping("/getAddData")
    @ResponseBody
    public ResponseData getAddData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto addDataDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("PARENT_ID",shiroUser.getORG_ID());
        List<Dto> orglist = this.gwei022Service.listOrg(inDto);
        if(orglist.size() > 0){
            //Dto azp  = this.gwpService.getAzpFile(orglist.get(0).getString("BIZ_CODE"));
            addDataDto.put("GWEPMLEGAL",orglist.get(0).getString("ID"));
            addDataDto.put("GWEPMLEGAL_NAME",orglist.get(0).getString("NAME"));
            addDataDto.put("GWEPPLANT",orglist.get(0).getString("BIZ_CODE"));
            //addDataDto.put("GWEPPLANT_NAME",azp.getString("AZP02"));
        }
        addDataDto.put("GWEPDOCNO","HXDH" + DateUtils.getDate("yyyyMMddSSSSS"));//核销单号
        addDataDto.put("GWEP004",shiroUser.getID());
        addDataDto.put("GWEP005",shiroUser.getORG_ID());
        addDataDto.put("GWEP005_NAME",shiroUser.getSysOrgT().getNAME());
        addDataDto.put("GWEPDOCDATE", DateUtils.formatDate(new Date(), "yyyy-MM-dd"));//单据日期
        return new SuccessResponseData(addDataDto);
    }

    /**
     * 客户开窗
     */
    @RequestMapping("/getGwakData")
    @ResponseBody
    public ResponseData getGwakData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> resultDto = this.gwei022Service.getGwakData(page, inDto);
        return new PageReData(page, resultDto);
    }

    /**
     * 回款单开窗
     */
    @RequestMapping("/getGweoData")
    @ResponseBody
    public ResponseData getGweoData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> resultDto = this.gwei022Service.getGweoPage(page, inDto);
        return new PageReData(page, resultDto);
    }

    /**
     * 获取客户税率等
     */
    @RequestMapping("/getGwak")
    @ResponseBody
    public ResponseData getGwak(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto gwakDto = this.gwei022Service.getGwak(inDto);
        return new SuccessResponseData(gwakDto);
    }

    /**
     * 发票信息开窗
     */
    @RequestMapping("/getGwefData")
    @ResponseBody
    public ResponseData getGwefData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> resultDto = this.gwei022Service.getGwefPage(page, inDto);
        return new PageReData(page, resultDto);
    }

    /**
     * 查询回款信息列表
     */
    @RequestMapping("/getGwepData")
    @ResponseBody
    public ResponseData getGwepData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> resultDto = this.gwei022Service.getGwepData(inDto);
        /*for(int i = 0;i<= resultDto.size()-1;i++){
            Dto azpDto = this.gwpService.getAzpFile(resultDto.get(i).getString("GWEPPLANT"));
            if(azpDto != null) {
                resultDto.get(i).put("GWEPPLANT_NAME", azpDto.getString("AZP02"));
            }else {
                resultDto.get(i).put("GWEPPLANT_NAME", "");
            }

        }*/
        return new SuccessResponseData(resultDto);
    }

    /**
     * 查询开票信息列表
     */
    @RequestMapping("/getGweqData")
    @ResponseBody
    public ResponseData getGweqData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> resultDto = this.gwei022Service.getGweqData(inDto);
        return new SuccessResponseData(resultDto);
    }

    /**
     * 核销单号开窗
     */
    @RequestMapping("/getGwecdocno")
    @ResponseBody
    public ResponseData getGwecdocno(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> resultDto = this.gwei022Service.getGwecdocno(page, inDto);
        return new PageReData(page, resultDto);
    }
}
